/* 
 * (C) 2013 Daniel Garcia <contacto {at} danigarcia.org>
 * 
 * http://danigarcia.org
 * 
 * This file is part of BlackList.
 * 
 * BlackList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BlackList is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BlackList.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.danigarcia.android.blacklist.database;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.danigarcia.android.blacklist.R;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CallsListCursorAdapter extends CursorAdapter {

	private static final String TAG = SmsListCursorAdapter.class.getName();
	private static String dateFormat;
	
	public CallsListCursorAdapter(Context context, Cursor c) {
		super(context, c, true);
		dateFormat = context.getString(R.string.dateFormat);
	}
	
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) 
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		View rowItem = inflater.inflate(R.layout.listitem_call, parent, false);
		
		return rowItem;
	}
	
	@Override
	public void bindView(View view, Context context, Cursor cursor) 
	{
		TextView lblCallSender = (TextView)view.findViewById(R.id.lblCallSender);
		TextView lblSmsDate = (TextView)view.findViewById(R.id.lblCallDate);
		
		lblCallSender.setText(cursor.getString(cursor.getColumnIndex(DataManager.COLUMN_CALLS_PHONE)));
		
		long ms = Long.parseLong(cursor.getString(cursor.getColumnIndex(DataManager.COLUMN_CALLS_CREATIONDATE)));
		lblSmsDate.setText(msToDate(ms, dateFormat));
	}

	
	private String msToDate(long ms, String format)
	{
		DateFormat formatter = new SimpleDateFormat(dateFormat);
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(ms);
		 
		return formatter.format(calendar.getTime());
	}
}
